package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.linq.cool.beauty.domain.BeautyBrand;
import com.linq.cool.beauty.domain.BeautyBrandProductCategory;
import com.linq.cool.beauty.dto.BeautyBrandDTO;
import com.linq.cool.beauty.dto.BeautyBrandQueryDTO;
import com.linq.cool.beauty.service.BeautyBrandProductCategoryService;
import com.linq.cool.beauty.service.BeautyBrandService;
import com.linq.cool.beauty.vo.BeautyBrandVO;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.BeanUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yqlin
 * @Date: 2021/2/6 12:52
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("brand")
public class BeautyBrandController extends BaseController {
    @Autowired
    private BeautyBrandService beautyBrandService;
    @Autowired
    private BeautyBrandProductCategoryService beautyBrandProductCategoryService;

    @PreAuthorize(hasPerm = "beauty:brand:list")
    @GetMapping("list")
    public Result<?> list(BeautyBrandQueryDTO dto) {
        IPage<BeautyBrand> page = beautyBrandService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    @GetMapping(value = "{brandId}")
    public Result<?> getInfo(@PathVariable("brandId") Long brandId) {
        List<BeautyBrandProductCategory> brandProductCategories = beautyBrandProductCategoryService.list(new LambdaQueryWrapper<BeautyBrandProductCategory>()
                                                                                                                 .eq(BeautyBrandProductCategory::getBrandId, brandId)
        );
        List<Long> productCategoryIds = new ArrayList<>();
        for (BeautyBrandProductCategory brandProductCategory : brandProductCategories) {
            productCategoryIds.add(brandProductCategory.getProductCategoryId());
        }
        BeautyBrand brand = beautyBrandService.getById(brandId);
        BeautyBrandVO vo = new BeautyBrandVO();
        BeanUtils.copyProperties(brand, vo);
        vo.setProductCategoryIds(productCategoryIds);
        return Result.ok(vo);
    }

    @Log(moduleTitle = "美容品牌管理", businessType = BusinessType.INSERT)
    @PreAuthorize(hasPerm = "beauty:brand:add")
    @PostMapping
    public Result<?> add(@Validated @RequestBody BeautyBrandDTO dto) {
        return this.toResult(beautyBrandService.saveBrand(dto));
    }

    @Log(moduleTitle = "美容品牌管理", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "beauty:brand:edit")
    @PutMapping
    public Result<?> edit(@Validated @RequestBody BeautyBrandDTO dto) {
        return this.toResult(beautyBrandService.updateBrand(dto));
    }


    @Log(moduleTitle = "美容品牌管理", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPerm = "beauty:brand:remove")
    @DeleteMapping("{brandIds}")
    public Result<?> remove(@PathVariable("brandIds") List<Long> brandIds) {
        return this.toResult(beautyBrandService.removeByIds(brandIds));
    }

    /**
     * 获取所有--下拉列表
     *
     * @return
     */
    @GetMapping("getOptionSelect")
    public Result<?> getOptionSelect() {
        return Result.ok(beautyBrandService.list(new LambdaQueryWrapper<BeautyBrand>()
                                                         .select(BeautyBrand::getId, BeautyBrand::getName, BeautyBrand::getLogo)
        ));
    }

    /**
     * 根据商品分类获取所有旗下品牌
     *
     * @param productCategoryId
     *
     * @return
     */
    @GetMapping("getBrands/{productCategoryId}")
    public Result<?> getBrandsByProductCategoryId(@PathVariable("productCategoryId") Long productCategoryId) {
        // brand_id product_category_id
        List<BeautyBrandProductCategory> brandProductCategories = beautyBrandProductCategoryService.list(new LambdaQueryWrapper<BeautyBrandProductCategory>()
                                                                                                                 .eq(BeautyBrandProductCategory::getProductCategoryId, productCategoryId)
        );
        if (CollectionUtils.isEmpty(brandProductCategories)) {
            return Result.ok(new ArrayList<>(0));
        }
        // 根据 brand_id查询所有品牌
        // 取出所有品牌ID
        List<Long> brandIds = brandProductCategories.stream().map(BeautyBrandProductCategory::getBrandId).collect(Collectors.toList());
        return Result.ok(beautyBrandService.listByIds(brandIds));
    }
}
